using OpenBlock.Core;
using OpenBlock.Core.Event.PlayerControl;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.InputSystem;

namespace OpenBlock.Input.Handler
{
    public class KeyboardHandler : IInputHandler
    {
        public void HandleInputs(ref InputActions actions)
        {
            var eventQueue = GameManager.Instance.eventQueue;

            var mouse = Mouse.current;

            bool isPointerOnUI = EventSystem.current.IsPointerOverGameObject();

            if (!isPointerOnUI)
            {
                if (mouse.leftButton.wasPressedThisFrame) eventQueue.SendEvent(new DigEvent(DigEvent.Phase.Start));
                if (mouse.leftButton.wasReleasedThisFrame) eventQueue.SendEvent(new DigEvent(DigEvent.Phase.End));
                if (mouse.rightButton.wasPressedThisFrame) eventQueue.SendEvent(new PlaceEvent());

                //actions.select?.Invoke(mouse.scroll.ReadValue().y);
                float scrollY = mouse.scroll.ReadValue().y;
                if (!Mathf.Approximately(scrollY, 0))
                    eventQueue.SendEvent(new SelectItemEvent(SelectItemEvent.SelectMode.Offset, (int)Mathf.Sign(scrollY)));
            }

            // TODO: only actives in "InGame" mode
            if (!isPointerOnUI && Screen.safeArea.Contains(mouse.position.ReadValue()))
                eventQueue.SendEvent(new LookEvent(GameManager.Instance.settings.input.sensitivity * mouse.delta.ReadValue()));

            var keyboard = Keyboard.current;
            Vector2 movement = Vector2.zero;
            if (keyboard.aKey.isPressed) movement.x -= 1;
            if (keyboard.dKey.isPressed) movement.x += 1;
            if (keyboard.wKey.isPressed) movement.y += 1;
            if (keyboard.sKey.isPressed) movement.y -= 1;
            eventQueue.SendEvent(new MoveEvent(movement.normalized));

            if (keyboard.digit1Key.wasPressedThisFrame) eventQueue.SendEvent(new SelectItemEvent(SelectItemEvent.SelectMode.Index, 0));
            if (keyboard.digit2Key.wasPressedThisFrame) eventQueue.SendEvent(new SelectItemEvent(SelectItemEvent.SelectMode.Index, 1));
            if (keyboard.digit3Key.wasPressedThisFrame) eventQueue.SendEvent(new SelectItemEvent(SelectItemEvent.SelectMode.Index, 2));
            if (keyboard.digit4Key.wasPressedThisFrame) eventQueue.SendEvent(new SelectItemEvent(SelectItemEvent.SelectMode.Index, 3));
            if (keyboard.digit5Key.wasPressedThisFrame) eventQueue.SendEvent(new SelectItemEvent(SelectItemEvent.SelectMode.Index, 4));
            if (keyboard.digit6Key.wasPressedThisFrame) eventQueue.SendEvent(new SelectItemEvent(SelectItemEvent.SelectMode.Index, 5));
            if (keyboard.digit7Key.wasPressedThisFrame) eventQueue.SendEvent(new SelectItemEvent(SelectItemEvent.SelectMode.Index, 6));

            if (keyboard.spaceKey.isPressed) eventQueue.SendEvent(new JumpEvent());
            if (keyboard.leftShiftKey.isPressed) eventQueue.SendEvent(new DescendEvent());
            if (keyboard.xKey.wasPressedThisFrame) eventQueue.SendEvent(new ConfigureItemEvent()); 
            if (keyboard.escapeKey.wasPressedThisFrame) actions.menu?.Invoke();
        }

        
    }
}
